package org.idea.spring.condition.propertyinject;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Author linhao
 * @Date created in 8:20 上午 2021/5/8
 */
public class PropertyInjectDemo {

    @Autowired
    private Environment environment;


    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();

        //手动注入一些属性到Spring容器当中
        displaySpringContextEnvironmentProperties(annotationConfigApplicationContext.getEnvironment());

        annotationConfigApplicationContext.register(MyConfig.class);
        annotationConfigApplicationContext.register(PropertyInjectDemo.class);
        annotationConfigApplicationContext.refresh();
        //同一个对象
        Environment environment1 = annotationConfigApplicationContext.getBean(ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME,Environment.class);
        Environment environment2 = annotationConfigApplicationContext.getEnvironment();
//        String[] defaultProfiles = environment2.getDefaultProfiles();
//        System.out.println(defaultProfiles);
        System.out.println(environment1 == environment2);
        annotationConfigApplicationContext.getBean(PropertyInjectDemo.class);
//        PropertyInjectDemo propertyInjectDemo = annotationConfigApplicationContext.getBean(PropertyInjectDemo.class);
//        Resource resource = propertyInjectDemo.resource;
//        Scanner scanner = new Scanner(resource.getInputStream());
//        scanner.useDelimiter("\n");
//
//        while (scanner.hasNext()) {
//            System.out.println(scanner.nextLine());
//        }

        annotationConfigApplicationContext.close();
    }

    /**
     * 手动往spring容器中功能写入一些属性
     * @param configurableEnvironment
     */
    public static void displaySpringContextEnvironmentProperties(ConfigurableEnvironment configurableEnvironment){
        MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
        Map<String,Object> sources = new HashMap<>();
        sources.put("my.prop","exist2");
        sources.put("my.id","1");
        MapPropertySource mapPropertySource = new MapPropertySource("first-property-source",sources);
        mutablePropertySources.addFirst(mapPropertySource);
    }

}
